package com.lancoo.znbkxx.teacher.widget.newBarChart;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.highlight.Range;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dadong on 2020/3/19
 * Describe:
 */
public class CheckScoreBarChartRenderer extends BarChartRenderer {

    private RectF mBarShadowRectBuffer = new RectF();
    private boolean moreColors = false;

    public CheckScoreBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler, boolean moreColors) {
        super(chart, animator, viewPortHandler);
        this.mChart = chart;
        mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHighlightPaint.setColor(Color.rgb(0, 0, 0));
        mHighlightPaint.setAlpha(120);
        mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mShadowPaint.setStyle(Paint.Style.FILL);
        mBarBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBarBorderPaint.setStyle(Paint.Style.STROKE);
        this.moreColors = moreColors;
    }

    @Override
    protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#0ACD81"));
        colors.add(Color.parseColor("#5498FF"));
        colors.add(Color.parseColor("#FF9137"));
        colors.add(Color.parseColor("#F97878"));
        dataSet.setValueTextColors(colors);
        Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
        mBarBorderPaint.setColor(dataSet.getBarBorderColor());
        mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));
        final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;
        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();
        // draw the bar shadow before the values
        if (mChart.isDrawBarShadowEnabled()) {
            mShadowPaint.setColor(dataSet.getBarShadowColor());
            BarData barData = mChart.getBarData();
            final float barWidth = barData.getBarWidth();
            final float barWidthHalf = barWidth / 2.0f;
            float x;
            for (int i = 0, count = Math.min((int) (Math.ceil((float) (dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
                 i < count;
                 i++) {
                BarEntry e = dataSet.getEntryForIndex(i);
                x = e.getX();
                mBarShadowRectBuffer.left = x - barWidthHalf;
                mBarShadowRectBuffer.right = x + barWidthHalf;
                trans.rectValueToPixel(mBarShadowRectBuffer);
                if (!mViewPortHandler.isInBoundsLeft(mBarShadowRectBuffer.right)) {
                    continue;
                }
                if (!mViewPortHandler.isInBoundsRight(mBarShadowRectBuffer.left)) {
                    break;
                }
                mBarShadowRectBuffer.top = mViewPortHandler.contentTop();
                mBarShadowRectBuffer.bottom = mViewPortHandler.contentBottom();
                c.drawRect(mBarShadowRectBuffer, mShadowPaint);
            }
        }
        BarBuffer buffer = mBarBuffers[index];
        buffer.setPhases(phaseX, phaseY);
        buffer.setDataSet(index);
        buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
        buffer.setBarWidth(mChart.getBarData().getBarWidth());
        buffer.feed(dataSet);
        trans.pointValuesToPixel(buffer.buffer);
        final boolean isSingleColor = dataSet.getColors().size() == 1;

        if (isSingleColor) {
            mRenderPaint.setColor(dataSet.getColor());
        }

        for (int j = 0; j < buffer.size(); j += 4) {
            if (!isSingleColor) {
                if (dataSet.getEntryForIndex(j / 4).getY() > 140) {
                    Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#F37779"),
                            Color.parseColor("#DA1738"), Shader.TileMode.CLAMP);
                    mRenderPaint.setShader(shader);
                } else if (dataSet.getEntryForIndex(j / 4).getY() < 30) {
                    Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#70AAFD"),
                            Color.parseColor("#0D70FC"), Shader.TileMode.CLAMP);
                    mRenderPaint.setShader(shader);
                } else {
                    Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#71A4A7"),
                            Color.parseColor("#0E9355"), Shader.TileMode.CLAMP);
                    mRenderPaint.setShader(shader);
                }
            } else {
                if (moreColors) {
                    if (j / 4 == 0) {
                        Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#0ACD81"),
                                Color.parseColor("#B7F5BF"), Shader.TileMode.CLAMP);
                        mRenderPaint.setShader(shader);
                    } else if (j / 4 == 1) {
                        Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#5498FF"),
                                Color.parseColor("#89D3F6"), Shader.TileMode.CLAMP);
                        mRenderPaint.setShader(shader);
                    } else if (j / 4 == 2) {
                        Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#FF9137"),
                                Color.parseColor("#FFD59A"), Shader.TileMode.CLAMP);
                        mRenderPaint.setShader(shader);
                    } else if (j / 4 == 3) {
                        Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#F97878"),
                                Color.parseColor("#FFAFAF"), Shader.TileMode.CLAMP);
                        mRenderPaint.setShader(shader);
                    }

                } else {
                    Shader shader = new LinearGradient(0, buffer.buffer[j + 1], 0, buffer.buffer[j + 3], Color.parseColor("#39d5fe"),
                            Color.parseColor("#2699e7"), Shader.TileMode.CLAMP);
                    mRenderPaint.setShader(shader);
                }

            }

            //修改画笔的属性 是否圆头

            float wtch = buffer.buffer[j + 2] - buffer.buffer[j];
            mRenderPaint.setStrokeWidth(wtch);
            float height = buffer.buffer[j + 3] - buffer.buffer[j + 1];
            if (height != 0) {
                if (height > wtch) {
                    mRenderPaint.setStrokeCap(Paint.Cap.ROUND);
                    c.drawLine(buffer.buffer[j] + wtch / 2,
                            buffer.buffer[j + 1] + wtch / 2,
                            buffer.buffer[j + 2] - wtch / 2,
                            buffer.buffer[j + 3],
                            mRenderPaint);
                } else {
                    mRenderPaint.setStrokeCap(Paint.Cap.BUTT);
                    c.drawLine(buffer.buffer[j] + wtch / 2,
                            buffer.buffer[j + 1],
                            buffer.buffer[j + 2] - wtch / 2,
                            buffer.buffer[j + 3],
                            mRenderPaint);
                }
            }
        }
    }

    @Override
    public void drawHighlighted(Canvas c, Highlight[] indices) {
        BarData barData = mChart.getBarData();
        for (Highlight high : indices) {
            IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
            if (set == null || !set.isHighlightEnabled())
                continue;
            BarEntry e = set.getEntryForXValue(high.getX(), high.getY());
            if (!isInBoundsX(e, set))
                continue;
            Transformer trans = mChart.getTransformer(set.getAxisDependency());
            mHighlightPaint.setColor(set.getHighLightColor());
            mHighlightPaint.setAlpha(set.getHighLightAlpha());
            boolean isStack = high.getStackIndex() >= 0 && e.isStacked();
            final float y1;
            final float y2;
            if (isStack) {
                if (mChart.isHighlightFullBarEnabled()) {
                    y1 = e.getPositiveSum();
                    y2 = -e.getNegativeSum();
                } else {
                    Range range = e.getRanges()[high.getStackIndex()];
                    y1 = range.from;
                    y2 = range.to;
                }
            } else {
                y1 = e.getY();
                y2 = 0.f;
            }
            prepareBarHighlight(e.getX(), y1, y2, barData.getBarWidth() / 2f, trans);
            setHighlightDrawPos(high, mBarRect);
            mHighlightPaint.setStrokeCap(Paint.Cap.ROUND);
            mHighlightPaint.setStrokeWidth(mBarRect.right - mBarRect.left);
//            c.drawLine(mBarRect.centerX(), mBarRect.top, mBarRect.centerX(), mBarRect.bottom, mHighlightPaint);
        }
    }

    public interface ScoreClickListener {
        void onClick();
    }
}

